/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.abnf.validation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.antlr.runtime.EarlyExitException;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.diagnostics.Diagnostic;
import org.eclipse.xtext.nodemodel.SyntaxErrorMessage;
import org.eclipse.xtext.parser.antlr.ISyntaxErrorMessageProvider;
import org.eclipse.xtext.parser.antlr.SyntaxErrorMessageProvider;

import de.thwildau.tm.moses.abnf.abnf.Rule;
import de.thwildau.tm.moses.abnf.abnf.RuleSet;

/**
 * Provider for custom Syntax error messages of ABNF.
 * 
 * @author Thomas Kistel
 * @version $Revision: 762 $
 */
public class AbnfSyntaxErrorMessageProvider extends SyntaxErrorMessageProvider {

    /**
     * Provide the error message for a syntax error as specified in
     * {@link ISyntaxErrorMessageProvider#getSyntaxErrorMessage(IParserErrorContext)}. Provides a specific
     * syntax error message for Incremental Alternative Rules that are not supported.
     */
    public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) {
        EObject currentContext = context.getCurrentContext();
        RecognitionException e = context.getRecognitionException();
        /*
         * If the current context is a Rule, the Exception is a EarlyExitException and the corresponding Token
         * is a '/', then someone has tried to build an Incremental Alternative Rule. So we are providing a
         * specific error message here.
         */
        if (e != null && currentContext instanceof Rule) {
            if ("/".equals(e.token.getText()) && e instanceof EarlyExitException) {
                String errorMsg = "Incremental Alternatives Rules are not supported. Use: ruleset = alt1 / alt2 / alt3 / alt4 / alt5.";
                return new SyntaxErrorMessage(errorMsg, Diagnostic.SYNTAX_DIAGNOSTIC);
            }
            
            Rule rule = (Rule) currentContext;
            if(hasHexValueExpression(rule.getName())) {
                System.out.println("Rulename: " + rule.getName() + " is not allowed.");
            }
            
        }
        
        if(currentContext instanceof RuleSet ) {
            Token token = e.token;
            if(token != null && hasHexValueExpression(token.getText())) {
                String errorMsg = "'" + token.getText() + "' is not allowed as a rulename. Please specify a rulename that do not match a hex value (i.e. 'FF', 'F').";
                return new SyntaxErrorMessage(errorMsg, Diagnostic.SYNTAX_DIAGNOSTIC);
            }
        }
        
        return super.getSyntaxErrorMessage(context);
    }
    
    private boolean hasHexValueExpression(String ruleName) {
        Pattern pattern = Pattern.compile("[a-fA-F]");
        Matcher matcher = pattern.matcher(ruleName);
        return matcher.matches();
    }
}
